from sklearn.cluster import DBSCAN
import numpy as np
import matplotlib.pyplot as plt

# 生成示例数据
from sklearn.datasets import make_moons
X, _ = make_moons(n_samples=300, noise=0.05)

# DBSCAN 聚类
db = DBSCAN(eps=0.1, min_samples=5)
labels = db.fit_predict(X)

# 可视化结果
plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='rainbow', s=10)
# 绘制噪声点
plt.scatter(X[labels == -1][:, 0], X[labels == -1][:, 1], color='black', s=10, label='Noise')
plt.title("DBSCAN Clustering")
plt.legend()
plt.show()
